#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import re

LANGS = {
		"ach"	:"Acoli",
		"af"	:"Afrikaans",
		"af_ZA"	:"Afrikaans (South Africa)",
		"ak"	:"Akan",
		"sq"	:"Albanian",
		"sq_AL"	:"Albanian (Albania)",
		"aln"	:"Albanian Gheg",
		"am"	:"Amharic",
		"am_ET"	:"Amharic (Ethiopia)",
		"ar"	:"Arabic",
		"ar_SA"	:"Arabic (Saudi Arabia)",
		"ar_AA"	:"Arabic (Unitag)",
		"an"	:"Aragonese",
		"hy"	:"Armenian",
		"hy_AM"	:"Armenian (Armenia)",
		"as"	:"Assamese",
		"as_IN"	:"Assamese (India)",
		"ast"	:"Asturian",
		"az"	:"Azerbaijani",
		"az_AZ"	:"Azerbaijani (Azerbaijan)",
		"bal"	:"Balochi",
		"eu"	:"Basque",
		"eu_ES"	:"Basque (Spain)",
		"be"	:"Belarusian",
		"be_BY"	:"Belarusian (Belarus)",
		"be@tarask"	:"Belarusian (Tarask)",
		"bn"	:"Bengali",
		"bn_BD"	:"Bengali (Bangladesh)",
		"bn_IN"	:"Bengali (India)",
		"brx"	:"Bodo",
		"bs"	:"Bosnian",
		"bs_BA"	:"Bosnian (Bosnia and Herzegovina)",
		"br"	:"Breton",
		"bg"	:"Bulgarian",
		"bg_BG"	:"Bulgarian (Bulgaria)",
		"my"	:"Burmese",
		"my_MM"	:"Burmese (Myanmar)",
		"ca"	:"Catalan",
		"ca_ES"	:"Catalan (Spain)",
		"ca@valencia"	:"Catalan (Valencian)",
		"hne"	:"Chhattisgarhi",
		"cgg"	:"Chiga",
		"zh"	:"Chinese",
		"zh_CN"	:"Chinese (China)",
		"zh_CN.GB2312"	:"Chinese (China) (GB2312)",
		"zh_HK"	:"Chinese (Hong Kong)",
		"zh_TW"	:"Chinese (Taiwan)",
		"zh_TW.Big5"	:"Chinese (Taiwan) (Big5) ",
		"kw"	:"Cornish",
		"co"	:"Corsican",
		"crh"	:"Crimean Turkish",
		"hr"	:"Croatian",
		"hr_HR"	:"Croatian (Croatia)",
		"cs"	:"Czech",
		"cs_CZ"	:"Czech (Czech Republic)",
		"da"	:"Danish",
		"da_DK"	:"Danish (Denmark)",
		"nl"	:"Dutch",
		"nl_BE"	:"Dutch (Belgium)",
		"nl_NL"	:"Dutch (Netherlands)",
		"dz"	:"Dzongkha",
		"dz_BT"	:"Dzongkha (Bhutan)",
		"en"	:"English",
		"en_AU"	:"English (Australia)",
		"en_CA"	:"English (Canada)",
		"en_IE"	:"English (Ireland)",
		"en_ZA"	:"English (South Africa)",
		"en_GB"	:"English (United Kingdom)",
		"en_US"	:"English (United States)",
		"eo"	:"Esperanto",
		"et"	:"Estonian",
		"et_EE"	:"Estonian (Estonia)",
		"fo"	:"Faroese",
		"fo_FO"	:"Faroese (Faroe Islands)",
		"fil"	:"Filipino",
		"fi"	:"Finnish",
		"fi_FI"	:"Finnish (Finland)",
		"frp"	:"Franco-Provençal (Arpitan)",
		"fr"	:"French",
		"fr_CA"	:"French (Canada)",
		"fr_FR"	:"French (France)",
		"fr_CH"	:"French (Switzerland)",
		"fur"	:"Friulian",
		"ff"	:"Fulah",
		"gd"	:"Gaelic, Scottish",
		"gl"	:"Galician",
		"gl_ES"	:"Galician (Spain)",
		"lg"	:"Ganda",
		"ka"	:"Georgian",
		"ka_GE"	:"Georgian (Georgia)",
		"de"	:"German",
		"de_DE"	:"German (Germany)",
		"de_CH"	:"German (Switzerland)",
		"el"	:"Greek",
		"el_GR"	:"Greek (Greece)",
		"gu"	:"Gujarati",
		"gu_IN"	:"Gujarati (India)",
		"gun"	:"Gun",
		"ht"	:"Haitian (Haitian Creole)",
		"ht_HT"	:"Haitian (Haitian Creole) (Haiti)",
		"ha"	:"Hausa",
		"he"	:"Hebrew",
		"he_IL"	:"Hebrew (Israel)",
		"hi"	:"Hindi",
		"hi_IN"	:"Hindi (India)",
		"hu"	:"Hungarian",
		"hu_HU"	:"Hungarian (Hungary)",
		"is"	:"Icelandic",
		"is_IS"	:"Icelandic (Iceland)",
		"ig"	:"Igbo",
		"ilo"	:"Iloko",
		"id"	:"Indonesian",
		"id_ID"	:"Indonesian (Indonesia)",
		"ia"	:"Interlingua",
		"ga"	:"Irish",
		"ga_IE"	:"Irish (Ireland)",
		"it"	:"Italian",
		"it_IT"	:"Italian (Italy)",
		"ja"	:"Japanese",
		"ja_JP"	:"Japanese (Japan)",
		"jv"	:"Javanese",
		"kn"	:"Kannada",
		"kn_IN"	:"Kannada (India)",
		"ks"	:"Kashmiri",
		"ks_IN"	:"Kashmiri (India)",
		"csb"	:"Kashubian",
		"kk"	:"Kazakh",
		"kk_KZ"	:"Kazakh (Kazakhstan)",
		"km"	:"Khmer",
		"km_KH"	:"Khmer (Cambodia)",
		"rw"	:"Kinyarwanda",
		"ky"	:"Kirgyz",
		"tlh"	:"Klingon",
		"ko"	:"Korean",
		"ko_KR"	:"Korean (Korea)",
		"ku"	:"Kurdish",
		"ku_IQ"	:"Kurdish (Iraq)",
		"lo"	:"Lao",
		"lo_LA"	:"Lao (Laos)",
		"la"	:"Latin",
		"lv"	:"Latvian",
		"lv_LV"	:"Latvian (Latvia)",
		"li"	:"Limburgian",
		"ln"	:"Lingala",
		"lt"	:"Lithuanian",
		"lt_LT"	:"Lithuanian (Lithuania)",
		"nds"	:"Low German",
		"lb"	:"Luxembourgish",
		"mk"	:"Macedonian",
		"mk_MK"	:"Macedonian (Macedonia)",
		"mai"	:"Maithili",
		"mg"	:"Malagasy",
		"ms"	:"Malay",
		"ml"	:"Malayalam",
		"ml_IN"	:"Malayalam (India)",
		"ms_MY"	:"Malay (Malaysia)",
		"mt"	:"Maltese",
		"mt_MT"	:"Maltese (Malta)",
		"mi"	:"Maori",
		"arn"	:"Mapudungun",
		"mr"	:"Marathi",
		"mr_IN"	:"Marathi (India)",
		"mn"	:"Mongolian",
		"mn_MN"	:"Mongolian (Mongolia)",
		"nah"	:"Nahuatl",
		"nr"	:"Ndebele, South",
		"nap"	:"Neapolitan",
		"ne"	:"Nepali",
		"ne_NP"	:"Nepali (Nepal)",
		"se"	:"Northern Sami",
		"nso"	:"Northern Sotho",
		"no"	:"Norwegian",
		"nb"	:"Norwegian Bokmål",
		"nb_NO"	:"Norwegian Bokmål (Norway)",
		"no_NO"	:"Norwegian (Norway)",
		"nn"	:"Norwegian Nynorsk",
		"nn_NO"	:"Norwegian Nynorsk (Norway)",
		"ny"	:"Nyanja",
		"oc"	:"Occitan (post 1500)",
		"or"	:"Oriya",
		"or_IN"	:"Oriya (India)",
		"pa"	:"Panjabi (Punjabi)",
		"pa_IN"	:"Panjabi (Punjabi) (India)",
		"pap"	:"Papiamento",
		"fa"	:"Persian",
		"fa_IR"	:"Persian (Iran)",
		"pms"	:"Piemontese",
		"pl"	:"Polish",
		"pl_PL"	:"Polish (Poland)",
		"pt"	:"Portuguese",
		"pt_BR"	:"Portuguese (Brazil)",
		"pt_PT"	:"Portuguese (Portugal)",
		"ps"	:"Pushto",
		"ro"	:"Romanian",
		"ro_RO"	:"Romanian (Romania)",
		"rm"	:"Romansh",
		"ru"	:"Russian",
		"ru_RU"	:"Russian (Russia)",
		"sm"	:"Samoan",
		"sc"	:"Sardinian",
		"sco"	:"Scots",
		"sr"	:"Serbian",
		"sr@latin"	:"Serbian (Latin)",
		"sr_RS@latin"	:"Serbian (Latin) (Serbia)",
		"sr_RS"	:"Serbian (Serbia)",
		"sn"	:"Shona",
		"sd"	:"Sindhi",
		"si"	:"Sinhala",
		"si_LK"	:"Sinhala (Sri Lanka)",
		"sk"	:"Slovak",
		"sk_SK"	:"Slovak (Slovakia)",
		"sl"	:"Slovenian",
		"sl_SI"	:"Slovenian (Slovenia)",
		"so"	:"Somali",
		"son"	:"Songhay",
		"st"	:"Sotho, Southern",
		"st_ZA"	:"Sotho, Southern (South Africa)",
		"es"	:"Spanish",
		"es_AR"	:"Spanish (Argentina)",
		"es_BO"	:"Spanish (Bolivia)",
		"es_CL"	:"Spanish (Chile)",
		"es_CO"	:"Spanish (Colombia)",
		"es_CR"	:"Spanish (Costa Rica)",
		"es_DO"	:"Spanish (Dominican Republic)",
		"es_EC"	:"Spanish (Ecuador)",
		"es_SV"	:"Spanish (El Salvador)",
		"es_MX"	:"Spanish (Mexico)",
		"es_NI"	:"Spanish (Nicaragua)",
		"es_PA"	:"Spanish (Panama)",
		"es_PY"	:"Spanish (Paraguay)",
		"es_PE"	:"Spanish (Peru)",
		"es_PR"	:"Spanish (Puerto Rico)",
		"es_ES"	:"Spanish (Spain)",
		"es_UY"	:"Spanish (Uruguay)",
		"es_VE"	:"Spanish (Venezuela)",
		"su"	:"Sundanese",
		"sw"	:"Swahili",
		"sw_KE"	:"Swahili (Kenya)",
		"sv"	:"Swedish",
		"sv_FI"	:"Swedish (Finland)",
		"sv_SE"	:"Swedish (Sweden)",
		"tl"	:"Tagalog",
		"tl_PH"	:"Tagalog (Philippines)",
		"tg"	:"Tajik",
		"tg_TJ"	:"Tajik (Tajikistan)",
		"ta"	:"Tamil",
		"ta_IN"	:"Tamil (India)",
		"ta_LK"	:"Tamil (Sri-Lanka)",
		"tt"	:"Tatar",
		"te"	:"Telugu",
		"te_IN"	:"Telugu (India)",
		"th"	:"Thai",
		"th_TH"	:"Thai (Thailand)",
		"bo"	:"Tibetan",
		"bo_CN"	:"Tibetan (China)",
		"ti"	:"Tigrinya",
		"to"	:"Tongan",
		"tr"	:"Turkish",
		"tr_TR"	:"Turkish (Turkey)",
		"tk"	:"Turkmen",
		"ug"	:"Uighur",
		"uk"	:"Ukrainian",
		"uk_UA"	:"Ukrainian (Ukraine)",
		"hsb"	:"Upper Sorbian",
		"ur"	:"Urdu",
		"ur_PK"	:"Urdu (Pakistan)",
		"uz"	:"Uzbek",
		"ve"	:"Venda",
		"vi"	:"Vietnamese",
		"vi_VN"	:"Vietnamese (Viet Nam)",
		"vls"	:"Vlaams",
		"wa"	:"Walloon",
		"cy"	:"Welsh",
		"cy_GB"	:"Welsh (United Kingdom)",
		"fy"	:"Western Frisian",
		"fy_NL"	:"Western Frisian (Netherlands)",
		"wo"	:"Wolof",
		"wo_SN"	:"Wolof (Senegal)",
		"xh"	:"Xhosa",
		"yi"	:"Yiddish",
		"yo"	:"Yoruba",
		"zu"	:"Zulu",
		"zu_ZA"	:"Zulu (South Africa)",
}



def getTranslatorLine(data, idx):
    # <b>English name (Native name)<br><a href=url>url</a>
    eName = ""
    nName = ""
    url = ""
    try:
        eName = data["translator_%s_nameEnglish" % idx]
    except KeyError:
        pass
       
    try:
        nName = data["translator_%s_nameNative" % idx]
    except KeyError:
        pass
       
    try:
        url = data["translator_%s_contact" % idx]
        url = url.replace("mailto:", "")
        url = url.replace("href", "")
    except KeyError:
        pass
    
    result = eName;
    
    if nName and nName != eName:
        result += " (%s)" % nName 

    result = "<b>%s</b><br>" % result
    if url: 
        if re.search('@', url):
            result += "<a href='mailto:%s'>%s</a>" % (url, url)
        else:
            result += "<a href='http:%s'>%s</a>" % (url, url)
    
    return result


translators = {}
for file in sys.argv[1:]:
    lang = re.search("_(.*)\.info", file).group(1)

    f = open(file, 'r')
    idxs = set()
    data = {}
    for line in f:
        line = line.strip()
        
        match = re.match("(translator_(\d*)_.*)\s*=\s(.*)", line)
        if match:
            value = match.group(3).strip()
            if value and value != "-":
                idxs.add(match.group(2))
                data[match.group(1).strip()] = match.group(3)

    #..............
    for i in idxs:
        s = getTranslatorLine(data, i) 
        try:
            translators[s] += ", " + LANGS[lang]
        except KeyError:
            translators[s] = LANGS[lang]
    
print "# -*- coding: utf-8 -*-"    
print "TRANSLATORS_TEXT=u'''"    
for key in sorted(translators.iterkeys()):
    print "%s<br>%s<p>" % (key, translators[key]) 
print "'''"


